﻿var dashBoardModel = new function () {

    this.Products = ko.observableArray(data.Products != null ? data.Products : []);
    this.TotalProduct = ko.computed(function () {
        return this.Products().length;
    }, this);
    this.HideCalendar = ko.observable(false);
    this.visibletoolbar = ko.observable(true);
    this.visiblepanel = ko.observable(true);

    this.expCalendar = function () {
        //alert(this.visibletoolbar());
        if (this.visiblecalendar()) {
            this.visiblecalendar(false);
            this.visibletoolbar(false);
            this.visiblepanel(false);
        }
        else {
            this.visiblecalendar(true);
            this.visibletoolbar(true);
            this.visiblepanel(true);
        }
    }
    this.expEvent = function () {
        //alert(this.visibletoolbar());
        if (this.visiblecalendar()) {
            this.visiblecalendar(false);
            this.visibletoolbar(false);
            this.visiblepanel(false);
        }
        else {
            this.visiblecalendar(true);
            this.visibletoolbar(true);
            this.visiblepanel(true);
        }
    }

    // Actions
    this.setProduct = function (item) {
        var data = { "id": item.ID };
        $.post("/Management/DashBoard/SetProductTransaction", data, function (model) {
            if (model != undefined && model != null) {
                dashBoardModel.Products(model.Products);
                paFlowModel.ProductApproved(model.ProductApproved);
            }
            $.fancybox.close();
        });
        return false;
    };
    this.DeleteProduct = function () {
        var data = { "id": $('#productId').val() };
        $.post("/Management/DashBoard/DeleteProductTransaction", data, function (model) {
            if (model != undefined && model != null) {
                dashBoardModel.Products(model.Products);
                //paFlowModel.ProductApproved(model.ProductApproved);
            }
            $.fancybox.close();
        });
    };
};

var paFlowModel = new function () {

    this.TimeUpdate = ko.observable(data.LastUpdated);
    this.ProductApproved = ko.observableArray(data.ProductApproved);
    this.visibletoolbar = ko.observable(true);
    this.visibleflow = ko.observable(true);
    this.maxFlow = ko.observable(50);
    
    this.crollExp = function () {
        if (!this.visibletoolbar()) {
            this.visibletoolbar(true);
            this.visibleflow(true);
        }

        if (this.setcreoll() == "scroll") {
            this.setcreoll("");
        }
        else {
            this.setcreoll("scroll");
        }
    }
    this.expFlow = function () {
        //alert(this.visibletoolbar());
        if (this.visibletoolbar()) {
            this.visibletoolbar(false);
            this.visibleflow(false);
            $('#expDashboardFlow').removeAttr("expander");
            $('#expDashboardFlow').attr("class", "unexpander");
        }
        else {
            this.visibletoolbar(true);
            this.visibleflow(true);
            $('#expDashboardFlow').removeAttr("unexpander");
            $('#expDashboardFlow').attr("class", "expander");
        }
    }
};

ko.applyBindings(dashBoardModel, document.getElementById("zoneCal"));
ko.applyBindings(paFlowModel, document.getElementById("zoneFlow"));
// Set CurrentDatetime for it
$('#startDate').blur(function () {
    setTimeout(function () {
        if (!isDateTime($('#startDate').val())) {
            $('#startDate').val(getCurrentDate());
        }
    }, 1);
});

$('#endDate').blur(function () {
    setTimeout(function () {
        if (!isDateTime($('#endDate').val())) {
            $('#endDate').val(getCurrentDate());
        }
    }, 1);
});

// Check datetime
function isDateTime(a) {
    var a = new Date(a);
    return ($.isNumeric(a.getTime()));
}

function DeleteDialog(id) {
    $('#productId').val(id);
    $('#productname').text($('#product' + id).attr("rel"));
}